package metrics

import (
	"log"
	"math/rand"
	"net"
	"strconv"
	"time"

	"github.com/prometheus/client_golang/prometheus"
)

// 创建一个Summary指标
var SummaryMetric = prometheus.NewSummaryVec(prometheus.SummaryOpts{
	Name:       "suber_example_summary",                                // 指标名称
	Help:       "一个 Summary 类型指标",                                      // 指标帮助信息
	Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, // 设置分位数和偏差
}, []string{"selfIP"})

func Summary() {
	timeStepInt, _ := strconv.Atoi(timeStep)
	addrs, _ := net.InterfaceAddrs()
	selfIP := ""
	for _, addr := range addrs {
		// 检查 IP 地址是否为回环地址
		if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil { // 优先返回 IPv4
				selfIP = ipnet.IP.String()
			}
		}
	}
	// 定期更新指标值
	go func() {
		for {
			a := rand.Float64() * 100
			log.Printf("Summary 随机浮点数 %v\n", a)
			SummaryMetric.WithLabelValues(selfIP).Observe(a)
			time.Sleep(time.Duration(timeStepInt) * time.Second)
		}
	}()
}
